# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         job51_utils
# Description:  提取51job页面的内容
# Author:       zhuchunxu
# Date:         2020/11/3
# -------------------------------------------------------------------------------
import datetime
import re
from logging import getLogger

from lxml import etree
from lxml.html import tostring

from spider_source.items import Job


class Job51Utils:
    def __init__(self):
        self.logger = getLogger(__name__)

    def get_page_detail_links(self, response):
        """
        获取当前页所有的详情页链接
        """
        html = etree.HTML(response.text)
        job_div_list = html.xpath("//div[@class='j_joblist']/div")
        cur_page_detail_link = []
        for div in job_div_list:
            a_list = div.xpath("./a")
            if len(a_list) > 0:
                href = a_list[0].attrib.get("href")
                cur_page_detail_link.append(href)

        return cur_page_detail_link

    def get_page_detail_info(self, response):
        html = etree.HTML(response.text)
        job = Job()

        try:
            # 获取岗位名称
            job["title"] = html.xpath("//div[@class='tCompanyPage']/div[2]/div[2]/div/div[@class='cn']/h1/text()")[0]
        except Exception as e:
            self.logger.error('提取岗位名称错误')

        # 职位链接
        job["job_url"] = response.url
        job["from_site"] = "www.jobs.51job.com-51job"

        # 公司名称
        try:
            job["job_company"] = \
                html.xpath("//div[@class='tCompanyPage']/div[2]/div[2]/div/div[@class='cn']/p/a[1]/text()")[0]
        except Exception as e:
            self.logger.error('提取公司名称错误')

        # 职位类别
        labels = html.xpath("//div[@class='tCompanyPage']/div[2]/div[3]/div[1]/div/div[1]/p/a/text()")
        job["category"] = ",".join(labels)

        if "兼职" in job["category"]:
            job["catalog"] = "兼职"
        else:
            job["catalog"] = "全职"

        # 职位描述
        try:
            job["job_desc"] = tostring(html.xpath("//div[@class='tCompanyPage']/div[2]/div[3]/div[1]/div")[0],
                                       encoding="utf-8").decode("utf-8")
        except Exception as e:
            self.logger.error('提取岗位内容错误:url:{},field:{}'.format(response.url, "job_desc"))

        job_require = html.xpath("//div[@class='tCompanyPage']/div[2]/div[2]/div/div[@class='cn']/p[2]/text()")
        if len(job_require) > 0:
            try:
                if "-" in job_require[0]:
                    job["city"] = job_require[0].split("-")[0].strip()
                    job["area"] = job_require[0].split("-")[1].strip()
                else:
                    job["city"] = job_require[0].strip()
                    job["area"] = job_require[0].strip()
            except Exception as e:
                self.logger.error('提取城市、区域等内容错误')

            if "人" in job_require[2]:
                job["education"] = job_require[1].strip()
                job["experience"] = ""
            else:
                job["experience"] = job_require[1].strip()
                job["education"] = job_require[2].strip()

            try:
                job["date"] = "{}-{}".format(datetime.datetime.now().year, job_require[-1][:-2].strip())
            except Exception as e:
                self.logger.error('提取发布日期错误')

        try:

            salary = html.xpath("//div[@class='tCompanyPage']/div[2]/div[2]/div/div[@class='cn']/strong/text()")
            if len(salary) > 0:
                salary = salary[0]
                salary_range = re.findall(r"\d+\.?\d*", salary)

                if len(salary_range) > 0:
                    job["salary_min"] = salary_range[0]
                    if "千" in salary:
                        job["salary_min"] = float(job["salary_min"]) * 1000

                    if "万" in salary:
                        job["salary_min"] = float(job["salary_min"]) * 10000

                if len(salary_range) > 1:
                    job["salary_max"] = salary_range[1]
                    if "千" in salary:
                        job["salary_max"] = float(job["salary_max"]) * 1000

                    if "万" in salary:
                        job["salary_max"] = float(job["salary_max"]) * 10000

        except Exception as e:
            self.logger.error('提取薪资错误')

        return job
